/**
 * 
 */
package com.ruyipu.forum.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.ruyipu.forum.R;
import com.ruyipu.forum.adapter.CommentAdapter;
import com.ruyipu.forum.base.Factory;
import com.ruyipu.forum.base.GlobalUtil;
import com.ruyipu.forum.base.Header;
import com.ruyipu.forum.interfaces.IItem;
import com.ruyipu.forum.item.Comment;
import com.ruyipu.forum.item.Discussion;
import com.ruyipu.forum.model.DiscussionModel;

/**
 * @author donald
 * 
 */
public class DiscussionActivity extends BaseActivity {
	public static final String TAG = DiscussionActivity.class.getSimpleName();
	public static final String NAME = DiscussionActivity.class.getName();

	private Discussion discussion;

	private List<IItem> dataList = new ArrayList<IItem>();
	private CommentAdapter myAdapter;
	private ListView lvDiscussion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Intent intent = getIntent();
		discussion = (Discussion) intent.getSerializableExtra(Discussion.TAG);
		if (null == discussion) {
			GlobalUtil.showShortToast(this, "no data");
			goBack(null);
		}

		dataList.add(discussion);

		setContentView(R.layout.discussion);

		super.onCreate(savedInstanceState);
		
		refresh(null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ruyipu.forum.activity.BaseActivity#makeWidget()
	 */
	@Override
	protected void makeWidget() {
		myHeader = new Header(this, discussion.getName());
		lvDiscussion = (ListView) findViewById(R.id.lvDiscussion);
	}

	@Override
	public void refresh(View v) {
		super.refresh(null);
		new MyDownloader().execute("");
	}

	private class MyDownloader extends AsyncDownloader {

		@Override
		protected JSONObject doInBackground(Object... params) {
			return DiscussionModel.getInstance().getDiscussionCommentList(
					DiscussionActivity.this, discussion.getId(), 20, 1);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ruyipu.forum.activity.BaseActivity#initWidget()
	 */
	@Override
	protected void initWidget() {
		myAdapter = new CommentAdapter(this, dataList);
		lvDiscussion.setAdapter(myAdapter);
		lvDiscussion.setLayoutAnimation(lacList);
	}

	// private void viewProfile() {
	// Intent intent = new Intent();
	// intent.setClassName(DiscussionActivity.this, UserProfile.NAME);
	// GlobalUtil.startBrotherActivity(DiscussionActivity.this, intent);
	// }

	@Override
	protected void updateData(JSONObject jsonObject) {
		if (null == jsonObject) {
			return;
		}
		List<IItem> tempList = Factory
				.factoryItemList(jsonObject, Comment.NAME);
		boolean dataChanged = false;
		while (!tempList.isEmpty()) {
			Comment newComment = (Comment) tempList.get(0);
			boolean commentExist = false;
			for (int i = 1; i < dataList.size(); i++) {
				Comment oldComment = (Comment) dataList.get(i);
				if (newComment.getId() == oldComment.getId()) {
					commentExist = true;
					if (!oldComment.getBody().equals(newComment.getBody())) {
						dataChanged = true;
						dataList.set(i, newComment);
					}
					break;
				}
			}
			if (!commentExist) {
				dataChanged = true;
				dataList.add(newComment);
			}
			tempList.remove(0);
		}
		if (dataChanged) {
			myAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void loadLocalData() {
		JSONObject jsonObject = DiscussionModel.getInstance()
				.loadLocalDiscussionComment(DiscussionActivity.this,
						discussion.getId(), 20, 1);
		updateData(jsonObject);
	}

}
